package com.azusa.xbjy.dao;

import com.azusa.xbjy.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {

    @Select("select * from `user` where username like concat('%',#{keywords},'%')")
    List<User> findByUserName(String keywords);

    @Select("SELECT COUNT(*) FROM userfocus WHERE user_id=#{id}")
    Integer selectUserFocusCountById(int id);

    @Select("SELECT user_focus_id FROM userfocus WHERE user_id=#{id}")
    List<Integer> findFocusedByUserId(int id);

    @Delete("DELETE FROM userfocus WHERE user_id=#{userId} AND user_focus_id=#{userFocusId}")
    int deleteUserfocus(int userId,int userFocusId);

    @Insert("INSERT INTO `userfocus`(`user_id`, `user_focus_id`) VALUES (#{userId}, #{userFocusId});")
    int addUserFocus(int userId,int userFocusId);

    @Update("UPDATE `user` SET look=look+1 WHERE id=#{id}")
    int updateLook(int id);

    @Select("<script>"
            + "SELECT" +
            "`user`.id, " +
            "`user`.username, " +
            "`user`.email, " +
            "`user`.real_name AS realName, " +
            "`user`.age, " +
            "`user`.phone, " +
            "`user`.gender, " +
            "`user`.info, " +
            "`user`.pic, " +
            "`user`.look, " +
            "`user`.is_secret, " +
            "`user`.dept_name, " +
            "`user`.dept_id, " +
            "`user`.register_time AS registerTime, " +
            "`user`.login_time AS loginTime" +
            " FROM user WHERE id IN "
            + "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach>"
            + "</script>")
    List<User> findMyUsers(@Param("ids")List<Integer> ids);

    @Select("SELECT COUNT(*) FROM userfocus WHERE user_focus_id=#{id}")
    int selectUserFocus(int id);

    @Insert("INSERT INTO `favorite`(`u_id`, `a_id`) VALUES (#{uid}, #{aid})")
    int insertCollect(int aid, int uid);

    @Delete("DELETE FROM `favorite` WHERE `u_id`=#{uid} AND `a_id`=#{aid}")
    int deleteCollect(int aid, int uid);

    @Select("SELECT COUNT(*) FROM `user` WHERE dept_id=#{deptId}")
    int selectDeptUserCount(int deptId);

    @Update("UPDATE `user` SET login_time=now() WHERE id=#{id}")
    int updateLoginTime(Integer id);
}
